package com.nidu.demo.file.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.file.convertor.FileConfigConvertor;
import com.nidu.demo.file.dto.FileConfigAddCmd;
import com.nidu.demo.file.dto.FileConfigCO;
import com.nidu.demo.file.gateway.FileConfigGateway;
import com.nidu.demo.file.model.FileConfig;
import com.nidu.demo.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文件配置添加命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class FileConfigAddCmdExe {

    private final FileConfigGateway fileConfigGateway;
    private final FileConfigConvertor fileConfigConvertor;

    @Transactional
    public SingleResponse<FileConfigCO> execute(FileConfigAddCmd cmd) {
        // 1. 转换为领域对象
        FileConfig fileConfig = FileConfig.create(cmd.getName(), cmd.getStorage(), cmd.getConfig(), cmd.getRemark());

        // 2. 保存文件配置
        FileConfig saved = fileConfigGateway.create(fileConfig);

        // 3. 转换为DTO返回
        FileConfigCO fileConfigCO = fileConfigConvertor.toClientObject(saved);
        return SingleResponse.of(fileConfigCO);
    }
}
